﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.IO;
using MotionCS.Controller;
using MotionCS.Config;
using MotionCS.Module;
using MotionCS.CameraVision;
using MotionCS.Enumerate;
using MotionCS.Helper;
using MotionCS.Alarm;
using MotionCS.StatusLed;

namespace MotionCS
{
    /// <summary>
    /// 相机取像配置
    /// </summary>
    public class CameraConf
    {
        /// <summary>
        /// Pcb属性
        /// </summary>
        public struct PcbAttri
        {
            /// <summary>
            /// 列数
            /// </summary>
            public int Column;
            /// <summary>
            /// 行数
            /// </summary>
            public int Row;
        }
        /// <summary>
        /// 光学设备类型
        /// </summary>
        public AoiDeviceTypeE AoiDeviceType;
        /// <summary>
        /// 相机名称
        /// </summary>
        public string CamName;
        /// <summary>
        /// 相机SN
        /// </summary>
        public string CamSn;
        /// <summary>
        /// 图片ID
        /// </summary>
        public int CamNo;
        /// <summary>
        /// 目标长
        /// </summary>
        public float Length;
        /// <summary>
        /// 目标宽
        /// </summary>
        public float Width;
        /// <summary>
        /// Pcb属性
        /// </summary>
        public PcbAttri pcbAttri;
        /// <summary>
        /// 光源类型
        /// </summary>
        public GrabLightTypeE LightType;
        /// <summary>
        /// 选拍点位
        /// </summary>
        public List<GrabPointConfig> GrabPoints = new List<GrabPointConfig>();
        /// <summary>
        /// 相机抓取回调
        /// </summary>
        public Action<string, string, bool> grabFinishCallback;
        /// <summary>
        /// x步距
        /// </summary>
        public float StepX;
        /// <summary>
        /// y步距
        /// </summary>
        public float StepY;
        /// <summary>
        /// 任务状态
        /// </summary>
        public ModuleTaskStateE TaskState;
    }


    public partial class MotionLib
    {
        /// <summary>
        /// 状态任务线程
        /// </summary>
        private Thread statusTask;
        /// <summary>
        /// 任务运行标识
        /// </summary>
        private bool hasRun = false;
        /// <summary>
        /// 图像数据回调
        /// </summary>
        private Action<IFrameData> imageDataCallback;
        /// <summary>
        /// 模组状态回调
        /// </summary>
        private Action<int, ModuleTaskStateE> moduleStateCallback;
        /// <summary>
        /// 轨道模组集合
        /// </summary>
        private List<ConveyorModule> conveyorModules;
        /// <summary>
        /// 相机模组集合
        /// </summary>
        private List<CameraModule> cameraModules;

        /// <summary>
        /// LED模组集合
        /// </summary>
        private List<LedControl> ledControls;
        /// <summary>
        /// 相机配置集合
        /// </summary>
        private List<CameraConf> cameraConfs;
        /// <summary>
        /// 报警检测模组
        /// </summary>
        private AlarmDetect alarmDetect;
        /// <summary>
        /// dip炉前相机
        /// </summary>
        private List<ICamera> dipFront;
        /// <summary>
        /// 默认构造函数
        /// </summary>
        public MotionLib()
        {
            conveyorModules = new List<ConveyorModule>();
            cameraModules = new List<CameraModule>();
            ledControls = new List<LedControl>();
            cameraConfs = new List<CameraConf>();
            alarmDetect = new AlarmDetect();
            dipFront = new List<ICamera>();
        }
        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        private Int32 MotionInit()
        {
            try
            {
                if (HardwareConfi.Instance.MotionConfi.AoiDeviceType == (int)AoiDeviceTypeE.DipFront)
                {
                    return InitDipFrontModule();
                }
                else
                {
                   MotionDevice.Instance.LogCallback = Logout;
                    MotionDevice.Instance.Init();
                    if (!MotionDevice.Instance.IsConnected())
                    {
                        return -1;
                    }

                    InitProcessModule();
                    InitAlarmModule();
                    hasRun = true;
                    statusTask = new Thread(new ThreadStart(StatusTaskFunction));
                    statusTask.IsBackground = true;
                    statusTask.Start();
                }
            }
            catch(Exception ex)
            {
                Logger.Logerr($"{ex}");
            }
            return 0;
        }
        /// <summary>
        /// 资源释放
        /// </summary>
        /// <returns></returns>
        public Int32 MotionUninit()
        {
            hasRun = false;
            conveyorModules.ForEach(itm => itm.Dispose());
            cameraModules.ForEach(itm => itm.Dispose());
            MotionDevice.Instance.Uninit();
            return 0;
        }

        /// <summary>
        /// 初始化炉前模组
        /// </summary>
        private int InitDipFrontModule()
        {
            if (HardwareConfi.Instance.CameraUpConfi.Enable == 1)
            {
                var camType = HardwareConfi.Instance.CameraUpConfi.CameraType;
                var camera = VisionDevice.Instance.GetCamera((CameraTypeE)camType);
                if (camera == null)
                {
                    Logout($"相机检索失败.");
                    return -1;
                }
                camera.LogoutCallBack = Logout;
                camera.ImageCallBack = imageDataCallback;
                if (camera.OpenDevice())
                {
                    camera.SetTriggerMode(false);
                    camera.StartGrab();
                }
                else
                {
                    Logout($"{camera.Info.strName}相机打开失败.");
                    return -1;
                }

                dipFront.Add(camera);
            }

            if (HardwareConfi.Instance.CameraDownConfi.Enable == 1)
            {
                var camType = HardwareConfi.Instance.CameraDownConfi.CameraType;
                var camera = VisionDevice.Instance.GetCamera((CameraTypeE)camType);
                if (camera == null)
                {
                    Logout($"相机检索失败.");
                    return -1;
                }
                camera.LogoutCallBack = Logout;
                camera.ImageCallBack = imageDataCallback;
                if (camera.OpenDevice())
                {
                    camera.SetTriggerMode(false);
                    camera.StartGrab();
                }
                else
                {
                    Logout($"{camera.Info.strName}相机打开失败.");
                    return -1;
                }

                dipFront.Add(camera);
            }

            for (int i = 0; i < dipFront.Count; i++)
            {
                cameraConfs.Add(new CameraConf()
                {
                    CamNo = i,
                    CamName = "",
                    CamSn = dipFront[i].Info.strSN,
                    AoiDeviceType = AoiDeviceTypeE.DipFront,
                });
            };

            return 0;
        }
        /// <summary>
        /// 初始化模组
        /// </summary>
        private void InitProcessModule()
        {
            if (HardwareConfi.Instance.ConveyorFrontConfi.Enable == 1)
            {
                ConveyorModule module = new ConveyorModule(new ConveyorModuleConfig()
                {
                    Name = "A轨",
                    ModuleKey = (int)ConveyorModuleE.Front,
                    LineMode = (LineModeE)HardwareConfi.Instance.ConveyorFrontConfi.LineMode,
                    Direction = (LineDirectionE)HardwareConfi.Instance.ConveyorFrontConfi.Direction,
                    SmemaMode = HardwareConfi.Instance.ConveyorFrontConfi.SmemaMode,
                    AxisT = AxisEnum.T_Front,
                    AxisW = AxisEnum.W_Front,
                    Cylinder1 = DoE.T1Cylinder1,
                    Cylinder2 = DoE.T1Cylinder2,
                    Cylinder3 = DoE.T1Cylinder3,
                    PassMode = DiE.T1PassMode,
                    Sensor1 = DiE.T1Sensor1,
                    Sensor2 = DiE.T1Sensor2,
                    Sensor3 = DiE.T1Sensor3,
                    Sensor4 = DiE.T1Sensor4,
                    DiFrontReady = DiE.T1FrontReady,
                    DiAfterReady = DiE.T1AfterReady,
                    DoFrontReady = DoE.T1FrontReady,
                    DoAfterReady = DoE.T1AfterReady,
                    DoCheckReady = DoE.T1CheckReady,
                    Timeout = HardwareConfi.Instance.ConveyorFrontConfi.Timeout,
                    AxisWPosition = HardwareConfi.Instance.ConveyorFrontConfi.WAxisPosition,
                    AxisWCheck = HardwareConfi.Instance.ConveyorFrontConfi.WAxisCheck,
                    ReaderConfi = HardwareConfi.Instance.ConveyorFrontConfi.ReaderConfi
                });
                if (module.IsEnable)
                {
                    conveyorModules.Add(module);
                }
            }

            if (HardwareConfi.Instance.ConveyorBackConfi.Enable == 1)
            {
                ConveyorModule module = new ConveyorModule(new ConveyorModuleConfig()
                {
                    Name = "B轨",
                    ModuleKey = (int)ConveyorModuleE.Back,
                    LineMode = (LineModeE)HardwareConfi.Instance.ConveyorBackConfi.LineMode,
                    Direction = (LineDirectionE)HardwareConfi.Instance.ConveyorBackConfi.Direction,
                    SmemaMode = HardwareConfi.Instance.ConveyorBackConfi.SmemaMode,
                    AxisT = AxisEnum.T_Back,
                    AxisW = HardwareConfi.Instance.ConveyorBackConfi.WAxisFixed == 0? AxisEnum.W_Back2 : AxisEnum.W_Back1,
                    Cylinder1 = DoE.T2Cylinder1,
                    Cylinder2 = DoE.T2Cylinder2,
                    Cylinder3 = DoE.T2Cylinder3,
                    PassMode = DiE.T2PassMode,
                    Sensor1 = DiE.T2Sensor1,
                    Sensor2 = DiE.T2Sensor2,
                    Sensor3 = DiE.T2Sensor3,
                    Sensor4 = DiE.T2Sensor4,
                    DiFrontReady = DiE.T2FrontReady,
                    DiAfterReady = DiE.T2AfterReady,
                    DoFrontReady = DoE.T2FrontReady,
                    DoAfterReady = DoE.T2AfterReady,
                    DoCheckReady = DoE.T2CheckReady,
                    Timeout = HardwareConfi.Instance.ConveyorBackConfi.Timeout,
                    AxisWPosition = HardwareConfi.Instance.ConveyorBackConfi.WAxisPosition,
                    AxisWCheck = HardwareConfi.Instance.ConveyorBackConfi.WAxisCheck,
                    ReaderConfi = HardwareConfi.Instance.ConveyorBackConfi.ReaderConfi
                });
                if (module.IsEnable)
                {
                    conveyorModules.Add(module);
                }
            }

            if (HardwareConfi.Instance.CameraUpConfi.Enable == 1)
            {
                CameraModule module = new CameraModule(new CameraModuleConfig()
                {
                    Name = "上照",
                    ModuleKey = (int)CameraModuleE.Upper,
                    GrabMode = (GrabModeE)HardwareConfi.Instance.CameraUpConfi.GrabMode,
                    LeftDlpGrabNum = HardwareConfi.Instance.CameraUpConfi.LeftDlpGrabNum,
                    FrontDlpGrabNum = HardwareConfi.Instance.CameraUpConfi.FrontDlpGrabNum,
                    RightDlpGrabNum = HardwareConfi.Instance.CameraUpConfi.RightDlpGrabNum,
                    BackDlpGrabNum = HardwareConfi.Instance.CameraUpConfi.BackDlpGrabNum,
                    AxisX = AxisEnum.X_Up,
                    AxisY = AxisEnum.Y_Up,
                    CameraType = (CameraTypeE)HardwareConfi.Instance.CameraUpConfi.CameraType,
                    CameraColor = (CameraColorE)HardwareConfi.Instance.CameraUpConfi.CameraColor,
                    CameraName = "",//HardwareConfi.Instance.CameraUpConfi.CameraSN, 
                    StrobeR = DoE.CameraUpStrobeR,
                    StrobeG = DoE.CameraUpStrobeG,
                    StrobeB = DoE.CameraUpStrobeB,
                    StrobeW = DoE.CameraUpStrobeW,
                    CamTrigger = DoE.CameraUpTrigger,
                    CamLeftDlp = DoE.CameraUpLeftDlp,
                    CamFrontDlp = DoE.CameraUpFrontDlp,
                    CamRightDlp = DoE.CameraUpRightDlp,
                    CamBackDlp = DoE.CameraUpBackDlp,
                    Calibration = HardwareConfi.Instance.CameraUpConfi.calibration,
                    ImageCallback = imageDataCallback,
                    ConveyorFront = HardwareConfi.Instance.ConveyorFrontConfi,
                    ConveyorBack = HardwareConfi.Instance.ConveyorBackConfi,
                });
                if (module.IsEnable)
                {
                    cameraModules.Add(module);
                }
            }

            if (HardwareConfi.Instance.CameraDownConfi.Enable == 1)
            {
                CameraModule module = new CameraModule(new CameraModuleConfig()
                {
                    Name = "下照",
                    ModuleKey = (int)CameraModuleE.Lower,
                    GrabMode = (GrabModeE)HardwareConfi.Instance.CameraDownConfi.GrabMode,
                    LeftDlpGrabNum = HardwareConfi.Instance.CameraDownConfi.LeftDlpGrabNum,
                    FrontDlpGrabNum = HardwareConfi.Instance.CameraDownConfi.FrontDlpGrabNum,
                    RightDlpGrabNum = HardwareConfi.Instance.CameraDownConfi.RightDlpGrabNum,
                    BackDlpGrabNum = HardwareConfi.Instance.CameraDownConfi.BackDlpGrabNum,
                    AxisX = AxisEnum.X_Down,
                    AxisY = AxisEnum.Y_Down,
                    CameraType = (CameraTypeE)HardwareConfi.Instance.CameraDownConfi.CameraType,
                    CameraColor = (CameraColorE)HardwareConfi.Instance.CameraDownConfi.CameraColor,
                    CameraName = "下",//HardwareConfi.Instance.CameraDownConfi.CameraSN,
                    StrobeR = DoE.CameraDownStrobeR,
                    StrobeG = DoE.CameraDownStrobeG,
                    StrobeB = DoE.CameraDownStrobeB,
                    StrobeW = DoE.CameraDownStrobeW,
                    CamTrigger = DoE.CameraDownTrigger,
                    CamLeftDlp = DoE.CameraDownLeftDlp,
                    CamFrontDlp = DoE.CameraDownFrontDlp,
                    CamRightDlp = DoE.CameraDownRightDlp,
                    CamBackDlp = DoE.CameraDownBackDlp,
                    Calibration = HardwareConfi.Instance.CameraDownConfi.calibration,
                    ImageCallback = imageDataCallback,
                    ConveyorFront = HardwareConfi.Instance.ConveyorFrontConfi,
                    ConveyorBack = HardwareConfi.Instance.ConveyorBackConfi,
                });
                if (module.IsEnable)
                {
                    cameraModules.Add(module);
                }
            }

            for (int i = 0; i < 4; i++)
            {
                CameraModule cameraModule = GetCameraModuleByCamNo(i);
                ConveyorModule conveyorModule = GetConveyorModuleByCamNo(i);
                if (cameraModule != null && conveyorModule != null)
                {
                    cameraConfs.Add(new CameraConf()
                    {
                        CamNo = i,
                        CamName = conveyorModule.ModuleName + cameraModule.ModuleName,
                        CamSn = cameraModule.Camera.Info.strSN,
                        StepX = cameraModule.StepX,
                        StepY = cameraModule.StepY,
                    });
                    //cameraModule.ResetStart();
                   // conveyorModule.ResetStart(false); //先不复位调宽
                }
            }

            LedControl led1 = new LedControl(new LedConfig()
            {
                ModuleKey = (int)ConveyorModuleE.Front,
                Red = DoE.T1Red,
                Green = DoE.T1Green,
                Yellow = DoE.T1Yellow,
                Beep = DoE.T1Beep,
            });
            if(led1.IsEnable)
            {
                ledControls.Add(led1);
            }

            LedControl led2 = new LedControl(new LedConfig()
            {
                ModuleKey = (int)ConveyorModuleE.Back,
                Red = DoE.T2Red,
                Green = DoE.T2Green,
                Yellow = DoE.T2Yellow,
                Beep = DoE.T2Beep
            });
            if(led2.IsEnable)
            {
                ledControls.Add(led2);
            }
        }
        /// <summary>
        /// 日志输出
        /// </summary>
        /// <param name="logtxt"></param>
        private void Logout(string logtxt)
        {
            Logger.Logout($"{logtxt}");
        }
        /// <summary>
        /// 模组复位
        /// </summary>
        private void ModuleReset()
        {
            ThreadHelper.Delay(1000);
            cameraModules.ForEach(itm => itm.ResetStart());
            conveyorModules.ForEach(itm => itm.ResetStart(false));
        }
        /// <summary>
        /// 初始化报警模组
        /// </summary>
        private void InitAlarmModule()
        {
            alarmDetect.LogoutCallback = Logout;
            InitAirPressureAlarm();
            InitSafeDoorAlarm();
            InitEmgStopAlarm();
        }
        /// <summary>
        /// 初始化气压报警
        /// </summary>
        private void InitAirPressureAlarm()
        {
            var alarm = new AlarmItem();
            alarm.ErrorID = (int)ErrorCodeE.AirPressure;
            alarm.Message = $"气压异常";
            alarm.Level = AlarmLevelE.Fatal;
            alarm.IsAlarmCallback = new Func<bool>(() =>
            {
                var state = MotionDevice.Instance.DiBit(DiE.Pressure);
                return state == null ? false : state.IsOn();
            });
            alarmDetect.AlarmItems.Add(alarm);
        }
        /// <summary>
        /// 初始化安全门报警
        /// </summary>
        private void InitSafeDoorAlarm()
        {
            var alarm = new AlarmItem();
            alarm.ErrorID = (int)ErrorCodeE.SafeDoor;
            alarm.Message = $"安全门打开";
            alarm.Level = AlarmLevelE.Fatal;
            alarm.IsAlarmCallback = new Func<bool>(() =>
            {
                var state = MotionDevice.Instance.DiBit(DiE.SafeDoor);
                if (!MotionDevice.Instance.m_bUseSafedoor)
                    return false;                                 //不使用就返回false;
                return state == null ? false : state.IsOff();
            });
            alarm.AlarmEndedCallback = new Action(() =>
            {
              //  ModuleReset();
            });
            alarmDetect.AlarmItems.Add(alarm);
        }
        /// <summary>
        /// 初始化急停开关报警
        /// </summary>
        private void InitEmgStopAlarm()
        {
            var alarm = new AlarmItem();
            alarm.ErrorID = (int)ErrorCodeE.EmgStop;
            alarm.Message = $"急停开关";
            alarm.Level = AlarmLevelE.Fatal;
            alarm.IsAlarmCallback = new Func<bool>(() =>
            {
                var state = MotionDevice.Instance.DiBit(DiE.EmgStop);
                return state == null ? false : state.IsOn();
            });
            alarm.AlarmEndedCallback = new Action(() =>
            {
                ModuleReset();
            });
            alarmDetect.AlarmItems.Add(alarm);
        }

        /// <summary>
        /// 状态运行任务
        /// </summary>
        private void StatusTaskFunction()
        {
            while (hasRun)
            {
                try
                {
                    alarmDetect.AlarmForeach();
                    ModuleStateProcess();
                    
                    ThreadHelper.Delay(1);
                }
                catch (Exception ex)
                {
                    Logger.Logerr($"{ex}");
                }
            }
        }
        /// <summary>
        /// 模组状态处理
        /// </summary>
        private void ModuleStateProcess()
        {
            cameraConfs.ForEach(itm =>
            {
                ModuleTaskStateE current = ModuleTaskStateE.None;
                var camera = GetCameraModuleByCamNo(itm.CamNo);
                var conveyor = GetConveyorModuleByCamNo(itm.CamNo);
                if (camera.ModuleTaskState() == ModuleTaskStateE.Error 
                || conveyor.ModuleTaskState() == ModuleTaskStateE.Error)
                {
                    current = ModuleTaskStateE.Error;
                }
                else if (conveyor.ModuleTaskState() == ModuleTaskStateE.Pass)
                {
                    current = ModuleTaskStateE.Pass;
                }
                else if (camera.ModuleTaskState() == ModuleTaskStateE.Resetting
                || conveyor.ModuleTaskState() == ModuleTaskStateE.Resetting)
                {
                    current = ModuleTaskStateE.Resetting;
                }
                else if (camera.ModuleTaskState() == ModuleTaskStateE.Running
                && (conveyor.ModuleTaskState() == ModuleTaskStateE.Running 
                || camera.CurrentRunningConveyor() == conveyor.ModuleKey))
                {
                    current = ModuleTaskStateE.Running;
                }
                else if (conveyor.ModuleTaskState() == ModuleTaskStateE.Idle
                || camera.ModuleTaskState() == ModuleTaskStateE.Idle)
                {
                    current = ModuleTaskStateE.Idle;
                }
                
                if (current != itm.TaskState)
                {
                    if (ledControls.Count == 2)
                    {
                        LedStatusControl((ConveyorModuleE)conveyor.ModuleKey, current);
                    }
                    moduleStateCallback?.Invoke(itm.CamNo, current);
                    Logout($"{itm.CamName}:{itm.TaskState}==>{current}");
                    itm.TaskState = current;
                }
            });

            if (ledControls.Count == 1)
            {
                if (cameraConfs.Exists(it => it.TaskState == ModuleTaskStateE.Error))
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.Error);
                }           
                else if (cameraConfs.Exists(it => it.TaskState == ModuleTaskStateE.Resetting))
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.Resetting);
                }
                else if (cameraConfs.Exists(it => it.TaskState == ModuleTaskStateE.Running))
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.Running);
                }
                else if (cameraConfs.Exists(it => it.TaskState == ModuleTaskStateE.Pass))
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.Pass);
                }
                else if (cameraConfs.Exists(it => it.TaskState == ModuleTaskStateE.Idle))
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.Idle);
                }
                else
                {
                    LedStatusControl(ConveyorModuleE.Front, ModuleTaskStateE.None);
                }
            }

            ledControls.ForEach(itm =>
            {
                itm.UpdateStatus();
            });
        }

        private void LedStatusControl(ConveyorModuleE module, ModuleTaskStateE taskState)
        {
            LedControl ledctrl = ledControls.Find(itm => itm.ModuleKey == (int)module);
            if (ledctrl == null)
            {
                ledctrl = ledControls[0];
            }

            switch (taskState)
            {
                case ModuleTaskStateE.Idle:
                    ledctrl.SetWorkStatus(StateLedE.YellowOn);    // YellowOn
                    break;
                case ModuleTaskStateE.Running:
                    if (module == ConveyorModuleE.Front)
                    {
                        if (GetConveyorModuleByCamNo(0) != null)
                        {
                            if (GetConveyorModuleByCamNo(0).bOpenBeepbB)
                                ledctrl.SetWorkStatus(StateLedE.RedSlowBeepOn);
                            else
                                ledctrl.SetWorkStatus(StateLedE.GreenOn);
                        }
                        else
                            ledctrl.SetWorkStatus(StateLedE.GreenOn);
                    }
                    // ledctrl.SetWorkStatus(StateLedE.GreenOn);
                    break;
                case ModuleTaskStateE.Error:
                    ledctrl.SetWorkStatus(StateLedE.RedSlowBeepOn);
                    break;
                case ModuleTaskStateE.Resetting:
                    ledctrl.SetWorkStatus(StateLedE.On);
                    break;
                case ModuleTaskStateE.Pass:
                    ledctrl.SetWorkStatus(StateLedE.GreenOn);
                    break;
                case ModuleTaskStateE.None:
                    ledctrl.SetWorkStatus(StateLedE.Off);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 获取相机模组
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private CameraModule GetCameraModuleByCamNo(int camNo)
        {
            CameraModule module = null;
            switch (camNo)
            {
                case 0:
                    if (cameraModules.Count > 0)
                    {
                        module = cameraModules[0];
                    }
                    break;
                case 1:
                    if (cameraModules.Count == 1 && conveyorModules.Count == 2)
                    {
                        module = cameraModules[0];
                    }

                    if (cameraModules.Count == 2)
                    {
                        module = cameraModules[1];
                    }
                    break;
                case 2:
                    if (cameraModules.Count == 2)
                    {
                        module = cameraModules[0];
                    }
                    break;
                case 3:
                    if (cameraModules.Count == 2)
                    {
                        module = cameraModules[1];
                    }
                    break;
                default:
                    break;
            }

            return module;
        }
        /// <summary>
        /// 获取轨道模组
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        private ConveyorModule GetConveyorModuleByCamNo(int camNo)
        {
            ConveyorModule module = null;
            switch (camNo)
            {
                case 0:
                    if (conveyorModules.Count > 0)
                    {
                        module = conveyorModules[0];
                    }
                    break;
                case 1:
                    if (conveyorModules.Count == 1 && cameraModules.Count == 2)
                    {
                        module = conveyorModules[0];
                    }
                    if (conveyorModules.Count == 2)
                    {
                        module = conveyorModules[1];
                    }
                    break;
                case 2:
                    if (conveyorModules.Count == 2)
                    {
                        module = conveyorModules[1];
                    }
                    break;
                case 3:
                    if (conveyorModules.Count == 2)
                    {
                        module = conveyorModules[1];
                    }
                    break;
                default:
                    break;
            }

            return module;
        }

        private string GetModuleNameByCamNo(int camNo)
        {
            return cameraConfs.Find(p => p.CamNo == camNo).CamName;
        }
    }
}
